package com.coin.liu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coin.liu.pojo.OtherTeacherData;
import com.coin.liu.Service.teacherOtherDataService;
import com.coin.liu.Utils.PageUtils;
import com.coin.liu.Utils.ResultMessage;
import com.lhf.springcloud.utils.ExcelUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Controller
public class teacherOtherdatacontroller {

    @Autowired
    teacherOtherDataService teacherOtherDataService;
    @GetMapping("/teacherOtherdata")
    public String listUploadedFiles(Model model) throws IOException {
        return "project/dataCenter/otherTeacherData";
    }

    @ResponseBody
    @GetMapping("/teacher_list")
    @ApiOperation("老师的其他信息分页展示")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "分页的当前页码"),
            @ApiImplicitParam(name = "limit",value = "每页显示的条数"),
            @ApiImplicitParam(name = "PageUtils",value = "这是一个工具包,其中包括.page,limit,totalcount(数据的总条数),records(当前页的所有数据),numberstart(开始的页码序号),numberend(页码的结束序号)," +
                    "所有数据封装到pageutils，传到前端取出其中需要的数据即可"),
            @ApiImplicitParam(name = "idCard",value = "根据身份证号查询传入的身份证号")
    })
    public PageUtils<OtherTeacherData> getContentCategory(Long page, Long limit, @RequestParam(value = "idCard",defaultValue = "") String idCard){

        IPage<OtherTeacherData> ipage=new Page<>(page,limit);
        IPage<OtherTeacherData> pages = teacherOtherDataService.page(ipage, new QueryWrapper<OtherTeacherData>().like("id_Card", idCard));

        long total = pages.getTotal();
        List<OtherTeacherData> records = pages.getRecords();

        PageUtils<OtherTeacherData> pageUtils=new PageUtils<OtherTeacherData>(page,limit,total,records);

        for (OtherTeacherData re: records
             ) {
            System.out.println(re);
        }
        return pageUtils;

    }


    @RequestMapping("/teacherlist_add")
    @ResponseBody
    public ResultMessage teacherinfoadd(@RequestBody OtherTeacherData otherTeacherData){

        System.out.println(otherTeacherData);
        String uuid = teacherOtherDataService.findUUID();
        ResultMessage requestMessage=null;
        otherTeacherData.setId(uuid);
       // otherTeacherData.setProfessionalName("234");
        // otherTeacherData.setTeacherName("eee");
        int count = teacherOtherDataService.addthotherdata(otherTeacherData);
        System.out.println(otherTeacherData);
        if (count > 0) {
           requestMessage = new ResultMessage(200, "添加成功");
        } else {
            requestMessage = new ResultMessage(500, "添加失败");
        }
        return requestMessage;
    }

    @PostMapping("/upLoadTeacherOtherInfo")
    @ApiOperation("文件上传")
    @ApiImplicitParam(name = "file",value = "要上传的文件")
    public String handleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes){
        if(file.isEmpty()) {
            return "redirect:/teacherData/teacherBasicData";
        }
        List<OtherTeacherData> list1= new ArrayList<>();
        try {
            InputStream inputStream = file.getInputStream();
            List<List<Object>> list = ExcelUtils.getCourseListByExcel(inputStream, file.getOriginalFilename());
            inputStream.close();

            for (int i = 0; i < list.size(); i++) {
                List<Object> thotherDatalist = list.get(i);

                OtherTeacherData otherTeacherData = new OtherTeacherData();
                if (thotherDatalist.size()==0){
                    break;
                }
                otherTeacherData.setIdCard(thotherDatalist.get(0).toString());
                otherTeacherData.setTeacherName(thotherDatalist.get(1).toString());
                otherTeacherData.setTeacherTitle(thotherDatalist.get(2).toString());
                otherTeacherData.setProfessionalTitle(thotherDatalist.get(3).toString());
                otherTeacherData.setDoubleDivision(thotherDatalist.get(4).toString());
                otherTeacherData.setProfessionalLeader(thotherDatalist.get(5).toString());
                otherTeacherData.setProfessionalName(thotherDatalist.get(6).toString());
                otherTeacherData.setOccupationNature(thotherDatalist.get(7).toString());
                otherTeacherData.setRetire(thotherDatalist.get(8).toString());
                otherTeacherData.setDutyStatus(thotherDatalist.get(9).toString());
                otherTeacherData.setInstructor(thotherDatalist.get(10).toString());
                otherTeacherData.setInstructorClass(thotherDatalist.get(11).toString());

                list1.add(otherTeacherData);
                // 执行插入操作

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (list1.size()>0){
            teacherOtherDataService.saveBatch(list1);
        }
        //redirectAttributes.addFlashAttribute("message", "You successfully uploaded " + file.getOriginalFilename() + "!" );

        return "redirect:/teacherData/otherTeacherData";  //重定向
    }

}
